package cn.chaozhoubang.vmall.backend.controller.impl;

import cn.chaozhoubang.vmall.backend.annotation.AuthRequire;
import cn.chaozhoubang.vmall.backend.controller.OrderController;
import cn.chaozhoubang.vmall.backend.mapper.OrderGoodsMapper;
import cn.chaozhoubang.vmall.backend.mapper.OrderMapper;
import cn.chaozhoubang.vmall.backend.model.*;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Todo 洋了个洋写
 */
@RestController
@RequestMapping("/api/private/v1")
public class OrderControllerImpl implements OrderController {

    private OrderMapper orderMapper;
    private OrderGoodsMapper orderGoodsMapper;

    private JSONArray buildGoods(List<SpOrder> orders) {
        JSONArray array = new JSONArray();
        for(SpOrder order: orders) {
            final JSONObject object = new JSONObject();
            object.set("order_id", order.getOrderId())
                    .set("user_id", order.getUserId())
                    .set("order_number", order.getOrderNumber())
                    .set("order_price", order.getOrderPrice())
                    .set("order_pay", order.getOrderPay())
                    .set("is_send", order.getIsSend())
                    .set("trade_no", order.getTradeNo())
                    .set("order_fapiao_title", order.getOrderFapiaoTitle())
                    .set("order_fapiao_company", order.getOrderFapiaoCompany())
                    .set("order_fapiao_content", order.getOrderFapiaoContent())
                    .set("consignee_addr", order.getConsigneeAddr())
                    .set("pay_status", order.getPayStatus())
                    .set("create_time", order.getCreateTime())
                    .set("update_time", order.getUpdateTime());
            array.add(object);
        }
        return array;
    }

    private JSONObject buildMeta(int status, String msg) {
        final JSONObject meta = new JSONObject();
        meta.set("status", status);
        meta.set("msg", msg);
        return meta;
    }

    private JSONObject buildData(IPage<SpOrder> page) {
        final JSONObject data = new JSONObject();
        data.set("total", page.getTotal());
        data.set("pagenum", page.getCurrent());
        data.set("goods", buildGoods(page.getRecords()));
        return data;
    }

    private JSONObject buildResult(JSONObject meta, JSONObject data) {
        final JSONObject res = new JSONObject();
        res.set("meta", meta);
        res.set("data", data);
        return res;
    }

    @Override
    @AuthRequire
    @GetMapping(value = "/orders", produces = "application/json;charset=UTF-8")
    public String getOrders
            (@RequestParam(value = "query", required = false, defaultValue = "") String query, int pagenum, int pagesize) {
        Page<SpOrder> page = new Page<>(pagenum, pagesize);
        IPage<SpOrder> paging = null;
        if(!"".equals(query)) {
            final QueryWrapper<SpOrder> wrapper = new QueryWrapper<>();
            wrapper.like("order_number", "%" + query + "%");
            paging = orderMapper.selectPage(page, wrapper);
        }
        else paging = orderMapper.selectPage(page, null);
        final JSONObject data = buildData(paging);
        final JSONObject meta = buildMeta(200, "获取成功");
        return buildResult(meta, data).toString();
    }




    @Autowired
    public void setOrderGoodsMapper(OrderGoodsMapper orderGoodsMapper) {
        this.orderGoodsMapper = orderGoodsMapper;
    }

    @Autowired
    public void setOrderMapper(OrderMapper orderMapper) {
        this.orderMapper = orderMapper;
    }
}
